package io.finer.erp.jeecg.approve.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.approve.entity.ApproveFlow;
import io.finer.erp.jeecg.approve.entity.ApproveFlowInstance;
import io.finer.erp.jeecg.approve.entity.ApproveFlowNode;
import io.finer.erp.jeecg.approve.entity.ApproveRecord;
import io.finer.erp.jeecg.approve.service.ApproveFlowInstanceService;
import io.finer.erp.jeecg.approve.service.ApproveRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 审批流实例 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-14
 */
@Api(tags="审批流实例")
@RestController
@RequestMapping("/approve/approveFlowInstance")
public class ApproveFlowInstanceController {

    @Autowired
    ApproveFlowInstanceService approveFlowInstanceService;

    @Autowired
    ApproveRecordService approveRecordService;

    @AutoLog(value="审批流实例-分页列表查询")
    @ApiOperation(value="审批流实例-分页列表查询", notes="审批流实例-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(ApproveFlowInstance approveFlowInstance,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<ApproveFlowInstance> queryWrapper = QueryGenerator.initQueryWrapper(approveFlowInstance, req.getParameterMap());
        Page<ApproveFlowInstance> page = new Page<>(pageNo, pageSize);
        IPage<ApproveFlowInstance> pageList = approveFlowInstanceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "审批流实例-通过id查询")
    @ApiOperation(value="审批流实例-通过id查询", notes="审批流实例-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ApproveFlowInstance approveFlowInstance = approveFlowInstanceService.getById(id);
        if(approveFlowInstance==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(approveFlowInstance);
    }

    @AutoLog(value = "审批流记录通过主表ID查询")
    @ApiOperation(value="审批流记录通过主表ID查询", notes="审批流记录-通主表ID查询")
    @GetMapping(value = "/queryLineByMainId")
    public Result<?> queryLineListByMainId(@RequestParam(name="id",required=true) String id) {
        QueryWrapper<ApproveRecord> queryWrapper = new QueryWrapper<>();
        List<ApproveRecord> approveRecordList = approveRecordService.list(queryWrapper.eq("instance_id", id));
        return Result.ok(approveRecordList);
    }
}

